<?php 
include 'method/win.php';

if(!isset($_SESSION['user'])){
    show_msg("请先登录!","login.php");
}
if(isset($_GET['action'])){
    logout();
}
if(isset($_GET['page'])){
    $page = $_GET['page'];
}else {
    $page = 1;
}
if(isset($_GET['id'])){
//     微博
//     $_SESSION['weiboid'] = $_GET['id'];
    $wbsql = "SELECT weibo_user.*, weibo_list.* FROM weibo_user, weibo_list WHERE weibo_list.weibo_id=".$_GET['id']." AND weibo_user.user_name = weibo_list.author";
    $wbres = $conn->query($wbsql);
    $wbrow = $wbres->fetch_assoc();
}else {
//     微博
    $wbsql = "SELECT weibo_user.*, weibo_list.* FROM weibo_user, weibo_list WHERE weibo_user.user_name = weibo_list.author limit 1";
    $wbres = $conn->query($wbsql);
    $wbrow = $wbres->fetch_assoc();

}
//     评论
$cmsql = "SELECT weibo_user.user_tx, weibo_comment.* FROM weibo_comment, weibo_user WHERE weibo_comment.weibo_id=".$wbrow['weibo_id']." AND weibo_user.user_name = weibo_comment.cm_author";
$cmres = $conn->query($cmsql);

$cmper_page = 10;
if(!isset($_GET['cmpage'])){
    $cmpage = 1;
}else{
    $cmpage = $_GET['cmpage'];
}

$cmtotal_ly = $cmres->num_rows;
$cmtotal_page = ceil($cmtotal_ly/$cmper_page);

$cmsql = "SELECT weibo_user.user_tx, weibo_comment.* FROM weibo_comment, weibo_user WHERE weibo_comment.weibo_id=".$wbrow['weibo_id']." AND weibo_user.user_name = weibo_comment.cm_author order by cm_time desc limit ".($cmpage-1)*$cmper_page.",".$cmper_page;
$cmres = $conn->query($cmsql);
//     转发
$relsql = "SELECT weibo_list.*, weibo_user.user_tx FROM weibo_list, weibo_user WHERE relwb_id = ".$wbrow['weibo_id']." AND weibo_list.author = weibo_user.user_name order by public_time desc";
$relres = $conn->query($relsql);

$scsql = "select * from weibo_sc where user='".$_SESSION['user']."' and weibo_id='".$wbrow['weibo_id']."'";
$scres = $conn->query($scsql);
if($scres->num_rows){
    $sc = 1;
}else {
    $sc = 0;
}

$zansql = "select * from weibo_zan where user='".$_SESSION['user']."' and weibo_id='".$wbrow['weibo_id']."'";
$zanres = $conn->query($zansql);
if($zanres->num_rows){
    $zan = 1;
}else {
    $zan = 0;
}
// 发表评论
if(isset($_POST['comment-btn'])){
    comment($_SESSION['user'], $_POST['comment-tx'], date("Y-m-d H:i:s"), $wbrow['weibo_id']);
}
// 转发
if(isset($_POST['rel-btn'])){
    relweibo($_SESSION['user'], date("Y-m-d H:i:s"), $_POST['rel-txt'], $wbrow['weibo_id']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博详情</title>
    <link rel="stylesheet" href="../lib/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/detail.css">
    <script src="../lib/jquery/jquery.js" type="text/javascript"></script>
    <script src="../lib/bootstrap/js/bootstrap.js" type="text/javascript"></script>
</head>
<body>
<!--顶部导航条-->
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!--头部logo-->
        <div class="navbar-header">
            <a class="navbar-brand" href="#"><em><img src="../img/logo.jpg" height=30 width=30></em><em>小狗微博</em></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!--搜索框-->
            <form class="navbar-form navbar-left" role="search" method="post" action="search_loginafter.php">
                <div class="form-group">
                    <input type="text" class="form-control" style="width: 400px" placeholder="Search" name="str-search">
                </div>
                <button type="sumbit" class="btn btn-default" name="search-btn">搜索</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="index_loginafter.php">首页</a></li>
                <li><a href="person_loginafter.php"><?php echo $_SESSION['user']?></a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">我的消息 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="inbox.php?page=1">@我</a></li>
                        <li><a href="inbox.php?page=2">评论</a></li>
                        <li><a href="inbox.php?page=3">回复</a></li>
                        <li><a href="inbox.php?page=4">赞</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">个人中心 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                    	<li><a href="fixpw.php">修改密码</a></li>
                        <li><a href="fixtx.php">修改头像</a></li>
                        <li><a href="index_loginafter.php?action=logout">退出登录</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!--详情版心-->
<div class="detail-warp clearfix">
    <div class="left-warp">
        <!--微博板块-->
        <div class="weibo-box clearfix">
        	<?php 
//         	更多按钮
        	echo "<div class='btn-group'>";
        	echo "<button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>
                                更多 <span class='caret'></span></button>";
        	echo "<ul class='dropdown-menu'>";
        	if($wbrow['author']==$_SESSION['user']){
        	    echo "<li><a href='javascript:' onclick=delWb(".$wbrow['weibo_id'].")>删除</a></li>";
        	}else{
        	    echo "<li><a href='javascript:' onclick=comWb(".$wbrow['weibo_id'].")>投诉</a></li>";
        	}
        	echo "</ul></div>";
        	?>
            <!--作者头像-->
            <div class="author-tx">
                <img src="../<?php echo $wbrow['user_tx']?>" style="width: 50px; height: 50px; border-radius: 50%">
            </div>
            <!--内容版心-->
            <div class="content-warp">
                <!--用户名-->
                <div class="author-name">
                    <a href="person_loginafter.php?id=<?php echo $wbrow['user_id']?>"><?php echo $wbrow['author']?></a>
                </div>
                <!--发博时间-->
                <div class="weibo-time">
                    <?php echo $wbrow['public_time']?>
                </div>
                <!--微博内容-->
                <div class="weibo-content">
                    <?php echo $wbrow['weibo_content']?>
                </div>
            </div>
        </div>
        
        <?php 
        if($wbrow['relwb_id']==""){
            //         不是转发的
            echo "<div class='weibo-photo'>";
            if($wbrow['weibo_img']){
                echo "<img src='../".$wbrow['weibo_img']."' style='width: 500px; height: auto'>";
            }
            echo "</div>";
        }else {
            $relsql = "SELECT weibo_user.*, weibo_list.* FROM weibo_list, weibo_user WHERE weibo_user.user_name=weibo_list.author and weibo_id=".$wbrow['relwb_id']."";
            $relres = $conn->query($relsql);
            $relrow = $relres->fetch_assoc();
            
            //         转发的
            echo "<div class='rel-box'>";
            //         源用户名
            echo "<div class='author-name'><a href='person_loginafter.php?id=".$relrow['user_id']."'>".$relrow['author']."</a></div>";
            //         源微博内容
            echo "<a href='detail_loginafter.php?id=".$relrow['weibo_id']."'><div class='weibo-content'>
                ".$relrow['weibo_content']."
             </div>";
            //         源图片
            if($relrow['weibo_img']){
                echo "<div class='rel-photo'><img src='../".$relrow['weibo_img']."' style='width: auto; height: 167px'></div>";
            }
            //         源微博时间
            echo "<div class='rel-time'>".$relrow['public_time']."</div>";
            
            $rrsql = "select * from weibo_list where relwb_id = ".$relrow['weibo_id'];
            $rrres = $conn->query($rrsql);
            
            $rcsql = "select * from weibo_comment where weibo_id = ".$relrow['weibo_id'];
            $rcres = $conn->query($rcsql);
            $rkcsql = "select * from weibo_kidcomment where weibo_id = ".$relrow['weibo_id'];
            $rkcres = $conn->query($rkcsql);
            $rcnum = ($rcres->num_rows)+($rkcres->num_rows);
            
            //         源微博操作
            echo "<div class='rel-op'><span><em class='glyphicon glyphicon-share'></em><em>".$rrres->num_rows."</em></span>";
            echo "<span>|</span><span><em class='glyphicon glyphicon-comment'></em><em>".$rcnum."</em></span>";
            echo "<span>|</span><span><em class='glyphicon glyphicon-thumbs-up'></em><em>".$relrow['weibo_zan']."</em></span></div></a></div>";
            
        }
        
        $rsql = "select * from weibo_list where relwb_id = ".$wbrow['weibo_id'];
        $rres = $conn->query($rsql);
        
        $csql = "select * from weibo_comment where weibo_id = ".$wbrow['weibo_id'];
        $cres = $conn->query($csql);
        $ksql = "select * from weibo_kidcomment where weibo_id = ".$wbrow['weibo_id'];
        $kres = $conn->query($ksql);
        $cnum = ($cres->num_rows)+($kres->num_rows);
        ?>

        <!--微博操作-->
        <div class="weibo-op clearfix">
            <a href="javascript:" id="sc"><span><em class="glyphicon glyphicon-star-empty"></em><em>收藏</em></span></a>
            <a href="detail_loginafter.php?id=<?php echo $wbrow['weibo_id']?>&page=2" class="rel-btn"><span><em class="glyphicon glyphicon-share"></em><em><?php echo $rres->num_rows?></em></span></a>
            <a href="detail_loginafter.php?id=<?php echo $wbrow['weibo_id']?>&page=1" class="cm-btn" style="color: black"><span><em class="glyphicon glyphicon-comment"></em><em><?php echo $cnum?></em></span></a>
            <a href="javascript:" id="zan"><span><em class="glyphicon glyphicon-thumbs-up"></em><em><?php echo $wbrow['weibo_zan']?></em></span></a>
        </div>

		<div class="comment choose-2">
    		<!--评论板块-->
            <div class="comment-warp clearfix">
                <!--评论者头像-->
                <div class="cm-tx">
                    <img src="../<?php echo $wbrow['user_tx']?>" style="width: 30px; height: 30px">
                </div>
                <form method="post" action="" onsubmit="return checkNull()">
                    <!--评论者操作-->
                    <div class="cm-op clearfix">
                        <textarea id="comment-txt" rows="1" name="comment-tx"></textarea>
                        <button type="submit" name="comment-btn">评论</button>
                    </div>
                </form>
            </div>
			<!--评论列表-->
            <div class="cm-list">
            	<?php 
                    if($cmres->num_rows){
                        while ($row = $cmres->fetch_assoc()){
    //                         评论盒子
                            echo "<div class='comment-box clearfix'>";
                            echo "<div class='cm-tx'><img src='../".$row['user_tx']."' style='width: 30px; height: 30px'></div>";
    //                         评论内容版心
                            echo "<div class='cmct-warp'>";
    //                         评论内容
                            echo "<div class='cm-content'>".$row['cm_author'].":".$row['cm_content']."</div>";
    //                         评论日期
                            echo "<div class='cm-time clearfix'>".$row['cm_time']."";
                            if($row['cm_author']==$_SESSION['user']){
                                echo "<span><a href='kid_comment.php?id=".$row['cm_id']."'>回复</a>&nbsp;&nbsp;<a href='javascript:' onclick=delCm(".$row['cm_id'].")>删除</a></span></div>";
                            }else{
                                echo "<span><a href='kid_comment.php?id=".$row['cm_id']."'>回复</a>&nbsp;&nbsp;<a href='javascript:' onclick=comCm(".$row['cm_id'].")>投诉</a></span></div>";
                            }
                            
                            $kcsql = "SELECT weibo_kidcomment.* FROM weibo_comment, weibo_kidcomment WHERE weibo_kidcomment.cm_id=weibo_comment.cm_id AND weibo_comment.cm_id=".$row['cm_id']."";
                            $kcres = $conn->query($kcsql);
                            if($kcres->num_rows){
                                //                         子评论表
                                echo "<a href='kid_comment.php?id=".$row['cm_id']."'><div class='cmkid-list'>共".$kcres->num_rows."条评论</div></a>";
                            }
                            echo "</div></div>";
                            //         加载更多
                            if($cmpage!="1"){
                                if($cmpage==1){
                                    echo "<div class='loadmore clearfix'>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>首页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>上一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage+1)."'>";
                                    echo "<span>下一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>尾页</span>";
                                    echo "</a>";
                                    echo "</div>";
                                }else if($cmpage==$cmtotal_page){
                                    echo "<div class='loadmore clearfix'>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>首页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage-1)."'>";
                                    echo "<span>上一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>下一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>尾页</span>";
                                    echo "</a>";
                                    echo "</div>";
                                }else {
                                    echo "<div class='loadmore clearfix'>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>首页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage-1)."'>";
                                    echo "<span>上一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage+1)."'>";
                                    echo "<span>下一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>尾页</span>";
                                    echo "</a>";
                                    echo "</div>";
                                }
                            }
                        }  
                    }else {
                        echo "暂无评论";
                    }
                ?>
            </div>
		</div>
        
        
        
        <div class="rel">
            <!--转发板块-->
            <div class="comment-warp clearfix">
                <form method="post" action="">
                    <!--转发者操作-->
                    <div class="zf-op clearfix">
                        <textarea rows="3" name="rel-txt"></textarea>
                        <button name="rel-btn">转发</button>
                    </div>
                </form>
            </div>

            <!--转发附带内容列表-->
            <div class="cm-list">
            <?php 
            if($relres->num_rows){
                while ($row = $relres->fetch_assoc()){
                    //             评论盒子
                    echo "<div class='comment-box clearfix'>";
                    echo "<div class='cm-tx'><img src='../".$row['user_tx']."' style='width: 30px; height: 30px'></div>";
                    //             评论内容版心
                    echo "<div class='cmct-warp'>";
                    //             评论内容
                    echo "<div class='cm-content'>".$row['author'].":".$row['weibo_content']."</div>";
                    //             评论日期
                    echo "<div class='cm-time'>".$row['public_time']."</div></div></div>";
                }
            }else {
                echo "暂无转发";
            }

            ?>
            </div>
        </div>
    </div>
    <div class="right-warp">
        <div class="logo">
            <img src="../img/logo1.jpg" style="width: 260px; height: 260px">
        </div>
    </div>
</div>
</body>
<!-- 脚本 -->
<script>
	var id = "<?php echo $wbrow['weibo_id']?>"
    var user = "<?php echo $_SESSION['user']?>"

    function checkNull(){
	    if($("#comment-txt").val()==""){
		    return false;
	    }
	    return true;
	}
   function delWb(id){
		var tipDelWb = confirm("你确定要删除此条微博？")
		if(tipDelWb){
			$.ajax({
                type:"post",
        		url:"method/delwb_action.php",
        		data:{id: id},
        		success:function(data){
        				alert(data)
        		}
            });
		}
	}
	function comWb(id){
		var tipComWb = confirm("你确定要投诉此条微博？")
		if(tipComWb){
			$.ajax({
                type:"post",
        		url:"method/complaintwb_action.php",
        		data:{id: id},
        		success:function(data){
        				alert(data)
        		}
            });
		}
	}
	function delCm(id){
		var tipDelCm = confirm("你确定要删除此条评论？")
		if(tipDelCm){
			$.ajax({
                type:"post",
        		url:"method/delcm_action.php",
        		data:{id: id},
        		success:function(data){
        				alert(data)
        		}
            });
		}
	}
	function comCm(id){
		var tipComCm = confirm("你确定要投诉此条评论？")
		if(tipComCm){
			$.ajax({
                type:"post",
        		url:"method/complaintcm_action.php",
        		data:{id: id},
        		success:function(data){
        				alert(data)
        		}
            });
		}
	}
        
    if(<?php echo $sc?>==1){
    	$("#sc").html("<span><em class='glyphicon glyphicon-star'></em><em>已收藏</em></span>")
    }

    if(<?php echo $zan?>==1){
    	$("#zan").css("color","red")
//     	alert("ok")
    }
    
    $("#sc").click(function () {
        if ($(this).text() == "收藏"){
                $.ajax({
                	type:"post",
        			url:"method/sc_action.php",
        			data:{id: id, user: user},
        			success:function(data){
        				alert(data);
        			}	
                });
            $(this).html("<span><em class='glyphicon glyphicon-star'></em><em>已收藏</em></span>")
        }else {
            var calsc = confirm("您确定要取消此微博的收藏吗？")
            if(calsc){
                    $.ajax({
                        type:"post",
                		url:"method/calsc_action.php",
                		data:{id: id, user: user},
                		success:function(data){
                			if(data=="取消收藏成功"){
                				$("#sc").html("<span><em class='glyphicon glyphicon-star-empty'></em><em>收藏</em></span>")
                				alert(data);
                			}
                		}	
                    });
            }
        }
    })
    $("#zan").click(function () {
        if(this.style.color == "red"){
        	$.ajax({
                type:"post",
        		url:"method/calzan_action.php",
        		data:{id: id, user: user},
        		success:function(data){
//         				alert(data)
        			}
            });
            this.style.color = "#888"
            location.reload();
        }else {
        	$.ajax({
                type:"post",
        		url:"method/zan_action.php",
        		data:{id: id, user: user},
        		success:function(data){
//         				alert(data)
        			}
            });
            this.style.color = "red"
            location.reload();
        }
    })
    if(<?php echo $page?> == "1"){
    	$(".rel-btn").css("color","#888")
        $(".cm-btn").css("color","black")
        $(".comment").removeClass("choose-2")
        $(".rel").removeClass("choose-2")
        $(".comment").addClass("choose-2")
    }else{
    	$(".cm-btn").css("color","#888")
        $(".rel-btn").css("color","black")
        $(".comment").removeClass("choose-2")
        $(".rel").removeClass("choose-2")
        $(".rel").addClass("choose-2")
    }
</script>
</html>